import java.util.*;

public class MemoryTest2 {
	public static int count = 1000;
	public static long freeMemory;
	public static long totalMemory;
	
	public static void main(String[] args) throws InterruptedException {
		System.out.println("Cold start");
		testArray();
		System.out.println();
		
		System.out.println("Array");
		testArray();
		System.out.println();
		
		System.out.println("AL");
		testAL();
		System.out.println();
		
		System.out.println("LL");
		testLL();
		System.out.println();
		
		System.out.println("HM");
		testHM();
		System.out.println();
		
		System.out.println("TM");
		testTM();
		System.out.println();
	}
	
	public static void testArray() {
		int i = 0;
		startMemory();
		
		int[] array = new int[count];
		for (i = 0; i < count; i++) {
			array[i] = i;
		}
		
		endMemory();
	}
	
	public static void testAL() {
		int i = 0;
		startMemory();
		
		List<Integer> array = new ArrayList<>(count);
		for (i = 0; i < count; i++) {
			array.add(i);
		}
		
		endMemory();
	}
	
	public static void testLL() {
		int i = 0;
		startMemory();
		
		List<Integer> array = new LinkedList<>();
		for (i = 0; i < count; i++) {
			array.add(i);
		}
		
		endMemory();
	}
	
	public static void testHM() {
		int i = 0;
		startMemory();
		
		Map<Integer, Integer> array = new HashMap<>(count);
		for (i = 0; i < count; i++) {
			array.put(i, i);
		}
		
		endMemory();
	}
	
	public static void testTM() {
		int i = 0;
		startMemory();
		
		Map<Integer, Integer> array = new TreeMap<>();
		for (i = 0; i < count; i++) {
			array.put(i, i);
		}
		
		endMemory();
	}
	
	public static void startMemory() {
		System.gc();
		freeMemory = Runtime.getRuntime().freeMemory();
		totalMemory = Runtime.getRuntime().totalMemory();
	}
	
	public static void endMemory() {
		System.gc();
		freeMemory = freeMemory - Runtime.getRuntime().freeMemory();
		totalMemory = totalMemory - Runtime.getRuntime().totalMemory();
		System.out.println(freeMemory);
		System.out.println(totalMemory);
	}
}
